package com.edgclub.edg.utils.GlideUtil;

import android.content.Context;

import com.bumptech.glide.load.model.GenericLoaderFactory;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.Headers;
import com.bumptech.glide.load.model.LazyHeaders;
import com.bumptech.glide.load.model.ModelCache;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.model.ModelLoaderFactory;
import com.bumptech.glide.load.model.stream.BaseGlideUrlLoader;

import java.io.InputStream;

public class TbxImageLoader extends BaseGlideUrlLoader<String> {


    public static class Factory implements ModelLoaderFactory<String, InputStream> {
        private final ModelCache<String, GlideUrl> modelCache = new ModelCache<String, GlideUrl>(500);

        @Override
        public ModelLoader<String, InputStream> build(Context context, GenericLoaderFactory factories) {
            return new TbxImageLoader(context, modelCache);
        }

        @Override
        public void teardown() {
        }
    }

    public TbxImageLoader(Context context, ModelCache<String, GlideUrl> modelCache) {
        super(context, modelCache);
    }

    @Override
    protected String getUrl(String model, int width, int height) {
        if(!model.contains("http://")&&!model.contains("https://")){
           return null;
        }
        return model;// Api.getPhotoURL(model, width, height);
    }

    @Override
    protected Headers getHeaders(String model, int width, int height) {
   //     return super.getHeaders(model, width, height);
        LazyHeaders.Builder builder = new LazyHeaders.Builder();
        builder.addHeader("referer","http://www.imguzhi.cn");
        return  builder.build();
    }
}